package com.yhd.exception;

import com.yhd.entity.result.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
//@ControllerAdvice
public class ControllerExceptionHandler {
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ResultVo<Object> handleException(Exception e) {
        String errorMsg = "";
        if (e instanceof NullPointerException) {
            errorMsg = "参数空指针异常";
        } else if (e instanceof HttpMessageNotReadableException) {
            errorMsg = "请求参数匹配错误," + e.getLocalizedMessage();
        } else {
            errorMsg = e.getMessage();
        }
        log.error(String.format("请求异常[%s]", e));
        ResultVo<Object> failure = ResultVo.failure(String.valueOf(HttpStatus.INTERNAL_SERVER_ERROR.value()), errorMsg);
        return failure;
    }
}
